/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.EstPriceBean;
import com.minierp.bean.EstPriceMultiBean;
import com.minierp.dao.EstPriceDao;
import com.minierp.dao.IdGenerator;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.EstPrice;
import com.minierp.model.Establishment;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class EstPriceMultiFormCtr extends SimpleFormController {
	private EstPriceDao estPriceDao;
	private IdGenerator idGenerator;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String estId = request.getParameter("est_id");
		String priceType = request.getParameter("price_type");
		String estType = request.getParameter("est_type");
		if (priceType != null && priceType.equals("EST")) {
			Establishment est = (Establishment) this.estPriceDao.get(
					Establishment.class, Long.parseLong(estId));
			refData.put("est", est);
		}

		refData.put("priceType", priceType);
		refData.put("estType", estType);
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		return new EstPriceMultiBean();
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
		String estId = request.getParameter("est_id");
		String priceType = request.getParameter("price_type");
		String estType = request.getParameter("est_type");
		EstPriceMultiBean bean = (EstPriceMultiBean) command;
		ArrayList arr = new ArrayList();
		int num = Integer.parseInt(request.getParameter("item_num"));

		for (int i = 0; i < num; ++i) {
			String status = request.getParameter("status_" + i);
			if (!status.equals("DELETE")) {
				EstPriceBean form = new EstPriceBean();
				EstPrice obj = new EstPrice();
				obj.setEstType(estType);
				obj.setPriceType(priceType);
				if (priceType != null && priceType.equals("EST")) {
					obj.setEstId(Long.parseLong(estId));
				}

				obj.setMakeTypeId(Long.parseLong(request.getParameter("mt_id_"
						+ i)));
				obj.setProId(Long.parseLong(request.getParameter("pro_id_" + i)));
				obj.setProName(request.getParameter("pro_name_" + i));
				obj.setLength(request.getParameter("length_" + i));
				obj.setWidth(request.getParameter("width_" + i));
				obj.setSpecification(request.getParameter("specification_" + i));
				obj.setMakeRequire(request.getParameter("mk_req_" + i));
				obj.setUnit(request.getParameter("unit_" + i));
				obj.setMark(request.getParameter("mark_" + i));
				form.setPriceStr(request.getParameter("price_" + i));
				form.setMtName(request.getParameter("mt_name_" + i));
				form.setEstPrice(obj);
				arr.add(form);
			}
		}

		bean.setItems(arr);
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		String estType = request.getParameter("est_type");
		EstPriceMultiBean bean = (EstPriceMultiBean) command;
		String alertMsg = "";
		String priCode = "";
		if (estType != null && estType.equals("CLIENT")) {
			priCode = "CLIE_PRIC";
		} else if (estType != null && estType.equals("PROVIDER")) {
			priCode = "PROV_PRIC";
		} else if (estType != null && estType.equals("COOPERATOR")) {
			priCode = "COOP_PRIC";
		}

		if (priCode.equals("")) {
			throw new Exception("Est type not supported!");
		} else if (EncCommonHelper.checkPrivilege(request, priCode)
				&& !EncCommonHelper.checkPurePrivilege(request, "NO_PRIC_EDIT")) {
			for (int mv = 0; mv < bean.getItems().size(); ++mv) {
				EstPrice obj = ((EstPriceBean) bean.getItems().get(mv))
						.getEstPrice();
				obj.setProSpell(PageHelper.getPinYinHeadChar(obj.getProName())
						.toUpperCase());
				obj.setId(this.idGenerator.getId(EstPrice.class.getName()));
				this.estPriceDao.save(obj);
				alertMsg = "增加成功！";
			}

			ModelAndView arg10 = new ModelAndView(this.getSuccessView());
			arg10.addObject("returnValue",
					String.valueOf(bean.getItems().size()));
			EncCommonHelper.setAlertMessage(request, alertMsg);
			return arg10;
		} else {
			throw new Exception("Permission deny!");
		}
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public EstPriceDao getEstPriceDao() {
		return this.estPriceDao;
	}

	public void setEstPriceDao(EstPriceDao estPriceDao) {
		this.estPriceDao = estPriceDao;
	}
}